package comp418.blingo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
 * 
 * @author Jingbang
 *
 */
public class description extends Activity {
	
	private Utility utility=null;
	private ArrayList<Button> bt_question=null;
	private Button bt_update;
	private TextView title;
	private TextView content;
	private TextView answercontent;
	private Button bt_coin;
	private Button bt_answer;
	private QuestionShow qs;
	private Context mContext;
	private int qid;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.description);
		utility=Utility.getUtility();
		mContext=this;
		
		qid=utility.getQid();
		//TODO: update description
		Log.i("test:","here");
		qs=utility.OpenQuestion(qid);
		Log.i("test:","hereasdf");
		title=(TextView)findViewById(R.id.questiontitle);
		content=(TextView)findViewById(R.id.displayContent);
		answercontent=(TextView)findViewById(R.id.displayAnswer);
		bt_update=(Button) findViewById(R.id.updatedetail);
		bt_update.setOnClickListener(new bt_up());
		bt_coin=(Button) findViewById(R.id.Give);
		bt_coin.setOnClickListener(new bt_give());
		//bt_coin.setClickable(false);
		bt_answer=(Button) findViewById(R.id.answer);
		bt_answer.setOnClickListener(new bt_answer());
		title.setText(qs.questionTitle);
		content.setText(qs.questionContent);
		Log.i("try",qs.answerContent);
		if(qs.answerContent!=null && !qs.answerContent.equals("")){
			answercontent.setText(qs.answerContent);
		}else
			answercontent.setText("No answer");
		/*if(qs.answerContent!=null){
			answercontent.setText(qs.answerContent);
			bt_coin.setClickable(true);
			bt_answer.setClickable(false);
		}
			*/
	}
	
	class bt_up implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			int currentID=0;
			currentID=utility.getQid();
			Intent intent=new Intent();
			String user=utility.getUsername();
			qs=utility.OpenQuestion(qid);
			//TODO: send the question id and refresh page
			title.setText(qs.questionTitle);
			content.setText(qs.questionContent);
			if(qs.answerContent!=null){
				answercontent.setText(qs.answerContent);
				bt_coin.setClickable(true);
				bt_answer.setClickable(false);
			}

		}
	}
	class bt_give implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			
		
			boolean result;
			result=utility.GiveCoin(qid);
			if(result){
				utility.showToast(mContext, "give one coin to the answer!");
				bt_coin.setClickable(false);
			}else
				utility.showToast(mContext, "fail");
			//TODO: send the question id to give coin and disable the give coin button
			
			

		}
	}
	class bt_answer implements android.view.View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			int currentID=0;
			currentID=utility.getQid();
			Intent intent=new Intent();
			String user=utility.getUsername();
			intent.putExtra("username", user);
			intent.setClass(description.this,answer.class);
			
			description.this.startActivity(intent);

		}
	}
	
}
